<template>
	<view >
		<!--  APP-PLUS 状态拦  -->
		<!-- <view class="status_bar">  
		    <view class="top_view"></view>  
		</view> -->  
		<!--  --> 
		<!-- <u-navbar title="修改密码" :fixed="false">  </u-navbar> -->
		 <view class="content">
			<u-form class="uform"  :rules="rules"  :model="form" ref="uForm" labelWidth="60">
				
				<!-- <h4 style="margin-top: 30rpx;">密码修改</h4> -->
				<u-form-item label="原密码:"  prop="password"  borderBottom>
					<u--input border="none"   v-model="form.password" 	placeholder="请输入"></u--input>
				</u-form-item>
				<u-form-item label="新密码:"  prop="Xpwd"  borderBottom>
					<u--input border="none"   v-model="form.Xpwd" 	placeholder="请输入"></u--input>
				</u-form-item>
				<u-form-item label="确认密码:"  prop="Qpwd"  borderBottom> 
					<u--input border="none"   v-model="form.Qpwd"	placeholder="请输入"></u--input>
				</u-form-item>
				<view class="submit" style="width: 80%; margin: 50rpx auto;">
					<u-button type="primary" color="#00ac4e" @click="submit_btn">提交</u-button>
				</view>
			</u-form>
		 </view>
	</view>
</template>

<script>
	import {
	    mapState 
	} from 'vuex';  
	export default {
		computed: {
			...mapState(['hasLogin','userInfo'])
		},
		data() {
			var validatePass = (rule, value, callback) => {
				if (value === '') {
					callback(new Error('请输入密码'));
				} else {
					// if (this.form.confirmPwd !== '') {
					// 	// this.$refs.uForm.validateField('confirmPwd');
					// 	callback(new Error('请输入新密码'));
					// }
					callback();
				}
			};
			var validatePass2 = (rule, value, callback) => {
				if (value === '') {
					callback(new Error('请再次输入密码'));
				} else if (value !== this.form.Xpwd) {
					callback(new Error('两次输入密码不一致!'));
				} else {
					callback();
				}
			};
			return {
				falses:true,
				bools:true,
				tips:"",
				disabled1: '',
				form:{
					Qpwd:"",
					Xpwd:"",
					password:"",
				},
				rules: {
					Phone:   [ {required: true,message: '请输入预留手机号',trigger: ['blur', 'change']},
					           {   // 自定义验证函数验证邮箱格式
					             	validator: (rule, value, callback) => {
					           		return uni.$u.test.mobile(value);
					           	},
					           	message: '手机号格式不正确',
					           	trigger: ['change','blur'],
					           },
					           { // 自定义验证函数，见上说明
								   validator: (rule, value, callback) => {
										// 上面有说，返回true表示校验通过，返回false表示不通过
										// uni.$u.test.mobile()就是返回true或者false的
										//console.log("this.userInfo.mobile:"+this.userInfo.mobile)
										if(value!=this.userInfo.mobile){
											this.disabled1=true;
											//alert(this.falses)
											return false
										}else{
											this.disabled1=false;
											//alert(this.falses)
											return true
										}
									
								    },
								   message: '预留手机号不正确',
								   // 触发器可以同时用blur和change  hasLogin
								   trigger: ['change','blur'],
					           }
					],
					Code:   [ {required: true,message: 'Code',trigger: ['blur', 'change']}],
					password:[ {required: true,message: '请输入密码',trigger: ['blur', 'change']},
					],
					 Xpwd:   [ {required: true,message: '请输入新密码',trigger: ['blur', 'change']},
					           { min: 6, max: 16, message: '长度在 6 到 12 个字符', trigger: 'blur' },
							   {validator: validatePass, trigger: 'blur' },],
					Qpwd:    [ {required: true,message: '请输入确认密码',trigger: ['blur', 'change']},
					           { min: 6, max: 16, message: '长度在 6 到 12 个字符', trigger: 'blur' },
					           {validator: validatePass2}], 
				}
			}
		},
		computed: {
			//获取是否登录
			...mapState(['hasLogin','userInfo']) 
		},
		methods: {
			submit_btn() {
				this.$refs.uForm.validate().then(res => {
					uni.$u.toast('校验通过'+this.userInfo.id)
					//ajx请求接口
					let datas={
						oldPassword:this.form.password,
						newPassword:this.form.Qpwd,
						technicianId:this.userInfo.id
					}
					console.log("修改返回datas:"+JSON.stringify(datas))
				   	this.request.postRequest(this.http.httpsLian.updatePassword,datas,true)
				   .then(res => { 
						console.log("修改返回:"+JSON.stringify(res))
						let iconMsg;
						if(res.code==0)iconMsg=true;
						if(res.code!=0)iconMsg=false;
						uni.showToast({
							title:iconMsg?"修改成功":'修改失败',
							icon:iconMsg?"success":"error",
							duration: 1800
						})
				   }).catch(res => {
				   	uni.$u.toast(res)
				   })
				}).catch(errors => {
						uni.$u.toast('校验失败：')
				}) 
			   
			},
			
			
		},
		onReady() {
           		this.$refs.uForm.setRules(this.rules);
        },
	}
</script>

<style lang="scss">

</style>
